﻿using AntdUI;
using Super.Admin.Global;
using Super.Admin.Properties;
using Super.Admin.Utils;
using Super.Admin.Utils.ThemeManager;
using System.Windows.Forms;

namespace Super.Admin.Views.Base
{
    /// <summary>
    /// 基础窗体类，继承自AntdUI.BaseForm
    /// </summary>
    public partial class BaseForm : AntdUI.BaseForm
    {
        public BaseForm()
        {
            InitializeComponent();
            this.Load += BaseForm_Load;
            this.DoubleBuffered = true; // 启用窗体级双缓冲
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); // 启用高级双缓冲
        }

        /// <summary>
        /// 窗体加载事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void BaseForm_Load(object? sender, EventArgs e)
        {
            RefreshTheme(this, ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.Theme.ThemeType);
            RefreshCornerRadius(this, ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.CornerRadiusType.CornerRadius); // 刷新圆角
            RefreshLanguage(this);  // 加载语言资源
            if (!DesignMode) this.Icon = ResourceHelper.Instance.LoadIcon(Global.AppContext.Instance.SuperLogoIcon); // 设置图标和Title
        }

        public void RefreshCornerRadius(Control control, int radius)
        {
            try
            {
                if (control == null) return;
                Type type = control.GetType();
                // 获取圆角属性
                var radiusProperty = type.GetProperty("Radius");
                if (radiusProperty != null && radiusProperty.CanWrite)
                {
                    if(Convert.ToInt32(radiusProperty.GetValue(control)) == radius)
                    {
                        radiusProperty.SetValue(control, radius);
                    }
                }
                foreach (Control child in control.Controls)
                {
                    RefreshCornerRadius(child, radius);
                }
            }
            catch (Exception ex)
            {
                AntdUI.Message.error(this, ex.Message);
            }
        }

        /// <summary>
        /// 更新窗体控件的语言资源
        /// </summary>
        /// <param name="control"></param>
        public void RefreshLanguage(Control control)
        {
            this.SuspendLayout();
            if (control is AntdUI.Button button)
            {
                button.Text = LanguageHelper.Instance.GetString(button.LocalizationText) ?? button.Text;
            }
            else if (control is AntdUI.Label label)
            {
                label.Text = LanguageHelper.Instance.GetString(label.LocalizationText) ?? label.Text;
            }
            else if (control is AntdUI.Input input)
            {
                input.PlaceholderText = LanguageHelper.Instance.GetString(input.LocalizationPlaceholderText) ?? input.PlaceholderText;
            }
            else if (control is AntdUI.Checkbox checkbox)
            {
                checkbox.Text = LanguageHelper.Instance.GetString(checkbox.LocalizationText) ?? checkbox.Text;
            }
            else if (control is AntdUI.Radio radio)
            {
                radio.Text = LanguageHelper.Instance.GetString(radio.LocalizationText) ?? radio.Text;
            }
            foreach (Control child in control.Controls)
            {
                RefreshLanguage(child);
            }
            this.ResumeLayout(false);
        }

        /// <summary>
        /// 更新主题
        /// </summary>
        /// <param name="control"></param>
        /// <param name="tAMode"></param>
        public void RefreshTheme(Control control, AntdUI.TAMode themeMode)
        {
            this.SuspendLayout();
            ThemeSettingHelper.Instance.SetControlsTheme(control, themeMode);
            this.ResumeLayout(false);
        }
    }
}
